package com.formula.datastructure.exercise.stack;

import com.formula.datastructure.util.DataUtil;

public class SqStack {

    // 一般不考虑扩容问题
    int SIZE = 50;

    int top = -1;

    Integer[] array = new Integer[SIZE];

    public boolean isEmpty() {
        if (top == -1) {
            return true;
        }
        return false;
    }

    /**
     * @param data
     * @return null means failed
     */
    public Integer push(Integer data) {
        if (top == array.length - 1) {
            return null;
        }
        array[++top] = data;
        return array[top];
    }

    public Integer pop() {
        Integer result = array[top];
        array[top--] = null;
        return result;
    }

    public static void main(String[] args) {
        SqStack stack = new SqStack();

        Integer[] input = DataUtil.genUniqueArray(10, 50);
        DataUtil.printArray(input);

        for (Integer data : input) {
            stack.push(data);
        }

        for (int i = 0; i < input.length / 2; i++) {
            System.out.println(stack.pop());
        }
    }
}
